package com.bdjk.bdmw.InfoGath.ws.position.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.bdjk.bdmw.InfoGath.common.dao.impl.JdbcDAOImpl;
import com.bdjk.bdmw.InfoGath.communication.server.tcp.netty.SendClientCommand;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;
import com.bdjk.bdmw.InfoGath.listener.InfoGathListener;
import com.bdjk.bdmw.InfoGath.ws.position.dao.PositionDAO;
import com.bdjk.bdmw.InfoGath.ws.position.dao.impl.PositionDAOImpl;
import com.bdjk.bdmw.InfoGath.ws.position.model.PositionModel;
import com.bdjk.bdmw.InfoGath.ws.position.service.PositionWS;
@Component("positionWS")

public class PositionWSImpl extends JdbcDAOImpl implements PositionWS {
	
	private static final Logger logger = Logger.getLogger(PositionWSImpl.class);
	
	@GET
	@Path("/test")
	@Produces(MediaType.APPLICATION_JSON)
	public String test() {
		return "aaa";
	}
	/*
	@GET
	@Path("/currentPosition")
	@Produces(MediaType.APPLICATION_JSON)
	public  PositionModel queryCurrentPosition(){
		PositionDAO dao=new PositionDAOImpl();
		List<Object> paramList=new ArrayList<Object>();
		return dao.queryCurrentPosition(paramList);
	}
	
	@GET
	@Path("/historyPosition")
	@Produces(MediaType.APPLICATION_JSON)
	public List<PositionModel> queryHistoryPosition(){
		PositionDAO dao=new PositionDAOImpl();
		List<Object> paramList=new ArrayList<Object>();
		return dao.queryHistoryPosition(paramList);
	}
	*/
	
	
	@GET
	@Path("/queryPosition/{ter_ident}/{before}/{after}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<PositionModel> queryHistoryPosition(@PathParam("ter_ident")String ter_ident,@PathParam("before") String before,@PathParam("after") String after) throws BusinessException{
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(before);
		paramList.add(after);
		paramList.add(ter_ident);
		PositionDAO pdao=new PositionDAOImpl();
		return pdao.queryHistoryPosition(paramList);
	}
	
	@GET
	@Path("/queryCurrentPosition/{ter_ident}")
	@Produces(MediaType.APPLICATION_JSON)
	public PositionModel queryCurrentPosition(@PathParam("ter_ident")String ter_ident) throws BusinessException{
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(ter_ident);
		PositionDAOImpl pdao=new PositionDAOImpl();
		return pdao.queryCurrentPosition(paramList);
	}
	
	@GET
	@Path("/queryBattery/{ter_ident}")
	@Produces(MediaType.APPLICATION_JSON)
	public String queryBattery(@PathParam("ter_ident")String ter_ident) throws BusinessException{
		List<Object> params=new ArrayList<Object>();
		params.add(ter_ident);
		String sql="SELECT electricity,ter_status FROM t_position_extension WHERE ter_ident=? ORDER BY id DESC LIMIT 1";
		List<Object> resultSet=executeResultSQL(sql,params);
		if(resultSet.size()<=0) return "no such terminal";
		List<Object> result=(List<Object>)resultSet.get(0);
		int electricity=(int)result.get(0);
		String status=(String)result.get(1);
		if((Integer.valueOf(status)&Integer.valueOf("00000001",16))==1){
			return status+"low battery!";
		}else{
			return electricity+" battery normal";
		}
		
	}
	
	
	
	
	//获取贝安贝手表的通信类型
	public String getCommType(String terIdent) throws BusinessException{
		String commType="";
		String sql="SELECT bab_comm_type FROM t_position_extension WHERE ter_ident=? ORDER BY id DESC LIMIT 1";
		List<Object> params = new ArrayList<Object>();
		params.add(terIdent);
		List<Object> resultSet=executeResultSQL(sql,params);
		for(Object it:resultSet){
			List<Object> result=(List<Object>) it;
			for(Object its:result){
				commType=(String) its;
			}
		}
		return commType;
	}
	
	
	
	
	@GET
	@Path("/setSOS/{ter_Ident}/{index}/{number}")
	@Produces(MediaType.APPLICATION_JSON)
	public String setSOS(@PathParam("ter_Ident")String terIdent,@PathParam("index")int index,@PathParam("number")String telNum) throws BusinessException {
		// TODO Auto-generated method stub
		String commType=getCommType(terIdent);
		StringBuffer sb=new StringBuffer();
		sb.append("["+commType+"*"+terIdent+"*0010*SOS"+index+","+telNum+"]");
		SendClientCommand sendClientCommand=new SendClientCommand();
		int result=sendClientCommand.send(terIdent, sb.toString());
		if(-1==result){
			return("no such terminal! "+sb.toString()+" "+commType);
		}else{
			return("success! "+sb.toString()+" "+commType);
		}
	}

	@GET
	@Path("/setSOS/{ter_Ident}/{number1}/{number2}/{number3}")
	@Produces(MediaType.APPLICATION_JSON)
	public String setSOS(@PathParam("ter_Ident")String terIdent,@PathParam("number1")String telNum1,@PathParam("number2")String telNum2,@PathParam("number3")String telNum3) throws BusinessException {
		// TODO Auto-generated method stub
		String commType=getCommType(terIdent);
		StringBuffer sb=new StringBuffer();
		sb.append("["+commType+"*"+terIdent+"*0027*SOS,"+telNum1+","+telNum2+","+telNum3+"]");
		SendClientCommand sendClientCommand=new SendClientCommand();
		int result=sendClientCommand.send(terIdent, sb.toString());
		if(-1==result){
			return("no such terminal! "+sb.toString()+" "+commType);
		}else{
			return("success! "+sb.toString()+" "+commType);
		}
	}
	
	@GET
	@Path("/setCenter/{ter_Ident}/{number}")
	@Produces(MediaType.APPLICATION_JSON)
	public String setCenter(@PathParam("ter_Ident")String terIdent,@PathParam("number")String telNum) throws BusinessException {
		// TODO Auto-generated method stub
		String commType=getCommType(terIdent);
		StringBuffer sb=new StringBuffer();
		sb.append("["+commType+"*"+terIdent+"*0012*CENTER,"+telNum+"]");
		SendClientCommand sendClientCommand=new SendClientCommand();
		int result=sendClientCommand.send(terIdent, sb.toString());
		if(-1==result){
			return("no such terminal! "+sb.toString()+" "+commType);
		}else{
			return("success! "+sb.toString()+" "+commType);
		}
	}
	
	@GET
	@Path("/setCenter/{slave}/{ter_Ident}/{number}")
	@Produces(MediaType.APPLICATION_JSON)
	public String setCenter(@PathParam("ter_Ident")String terIdent,@PathParam("number")String telNum,@PathParam("slave")String slave) throws BusinessException {
		// TODO Auto-generated method stub
		String commType=getCommType(terIdent);
		StringBuffer sb=new StringBuffer();
		sb.append("["+commType+"*"+terIdent+"*0011*"+slave+","+telNum+"]");
		SendClientCommand sendClientCommand=new SendClientCommand();
		int result=sendClientCommand.send(terIdent, sb.toString());
		if(-1==result){
			return("no such terminal! "+sb.toString()+" "+commType);
		}else{
			return("success! "+sb.toString()+" "+commType);
		}
	}
	
	@GET
	@Path("/setWhiteNum/{ter_Ident}/{number}")
	@Produces(MediaType.APPLICATION_JSON)
	public String setWhiteNum(@PathParam("ter_Ident")String terIdent,@PathParam("number")String telNum) throws BusinessException {
		// TODO Auto-generated method stub
		String commType=getCommType(terIdent);
		StringBuffer sb=new StringBuffer();
		sb.append("["+commType+"*"+terIdent+"*0016*WHITELIST1,"+telNum+"]");
		SendClientCommand sendClientCommand=new SendClientCommand();
		int result=sendClientCommand.send(terIdent, sb.toString());
		if(-1==result){
			return("no such terminal! "+sb.toString()+" "+commType);
		}else{
			return("success! "+sb.toString()+" "+commType);
		}
	}
	
	@GET
	@Path("/setMonitor/{ter_Ident}")
	@Produces(MediaType.APPLICATION_JSON)
	public String setMonitor(@PathParam("ter_Ident")String terIdent) throws BusinessException{
		String commType=getCommType(terIdent);
		StringBuffer sb=new StringBuffer();
		sb.append("["+commType+"*"+terIdent+"*0007*MONITOR]");
		SendClientCommand sendClientCommand=new SendClientCommand();
		int result=sendClientCommand.send(terIdent, sb.toString());
		if(-1==result){
			return("no such terminal! "+sb.toString()+" "+commType);
		}else{
			return("success! "+sb.toString()+" "+commType);
		}
	}
	
	@GET
	@Path("/Register/{ter_Ident}/{register_id}")
	@Produces(MediaType.APPLICATION_JSON)
	public String saveRegisterId(@PathParam("register_id")String register_id,@PathParam("ter_Ident")String ter_ident) throws BusinessException{
		String sql="insert into android_map values(?,?)";
		List<Object> params=new ArrayList<Object>();
		params.add(ter_ident);
		params.add(register_id);
		int result=executeSQL(sql,params);
		if(result!=-1){
			return "true";
		}
		else{
			return "false";
		}
	}
}
